package com.ruoyi.web.controller.product.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.VO.product.ProductSearchVO;
import com.ruoyi.common.entity.VO.product.ProductVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.product.service.IBackProductCategoryService;
import com.ruoyi.web.controller.product.service.IBackProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/back/product")
public class BackProductController extends BaseController {

    private String prefix = "product";

    @Resource
    private IBackProductService productService;

    @Resource
    private IBackProductCategoryService productCategoryService;

    /**
     * 页面-充值金额列表
     *
     * @return
     */
    @RequiresPermissions("back:product:memberRechargeList")
    @GetMapping("/memberRechargeList")
    public String memberRechargeList() {
        return prefix + "/memberRechargeList";
    }

    /**
     * 页面-油品列表
     *
     * @return
     */
    @RequiresPermissions("back:product:petrolList")
    @GetMapping("/petrolList")
    public String petrolList() {
        return prefix + "/petrolList";
    }

    /**
     * 页面-积分商城商品列表
     *
     * @return
     */
    @RequiresPermissions("back:product:creditList")
    @GetMapping("/creditList")
    public String creditList() {
        return prefix + "/creditList";
    }

    /**
     * 页面-商品树列表
     *
     * @return
     */
    @RequiresPermissions("back:product:list")
    @GetMapping("/tree")
    public String tree() {
        return prefix + "/tree";
    }

    /**
     * 页面-新增商品
     *
     * @return
     */
    @RequiresPermissions("back:product:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 页面-编辑商品
     *
     * @param productId 商品ID
     * @return
     */
    @RequiresPermissions("back:product:edit")
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap) {
        mmap.put("product", productService.getProductInfoById(productId));
        return prefix + "/edit";
    }

    /**
     * 页面-编辑商品分类
     *
     * @param productId 商品ID
     * @return
     */
    @RequiresPermissions("back:product:edit")
    @GetMapping("/editCategory/{productId}")
    public String editCategory(@PathVariable("productId") Long productId, ModelMap mmap) {
        mmap.put("productId", productId);
        return prefix + "/editCategory";
    }

    /**
     * 数据查询-积分商城商品列表（搜索）
     *
     * @param productSearchVO
     * @return
     */
    @RequiresPermissions("back:product:creditList")
    @PostMapping("/searchCreditList")
    @ResponseBody
    public TableDataInfo searchCreditList(ProductSearchVO productSearchVO) {
        startPage();
        productSearchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.CREDIT.getValue());
        List<ProductVO> list = productService.searchProductList(productSearchVO);
        for (ProductVO productVO : list
        ) {
            if (StringUtils.isNotEmpty(productVO.getCoverUrl())) {
                productVO.setCoverUrlThumb(productVO.getCoverUrl().concat("-tf70"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 数据查询-充值金额列表（搜索）
     *
     * @param productSearchVO
     * @return
     */
    @RequiresPermissions("back:product:memberRechargeList")
    @PostMapping("/searchMemberRechargeList")
    @ResponseBody
    public TableDataInfo searchMemberRechargeList(ProductSearchVO productSearchVO) {
        startPage();
        productSearchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue());
        productSearchVO.setIsDelete(0);
        List<ProductVO> list = productService.searchProductList(productSearchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-油品列表（搜索）
     *
     * @param productSearchVO
     * @return
     */
    @RequiresPermissions("back:product:petrolList")
    @PostMapping("/searchPetrolList")
    @ResponseBody
    public TableDataInfo searchPetrolList(ProductSearchVO productSearchVO) {
        startPage();
        productSearchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        productSearchVO.setIsDelete(0);
        List<ProductVO> list = productService.searchProductList(productSearchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-商品树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = productService.selectProductTree();
        return ztrees;
    }

    /**
     * 数据查询-校验原名称
     */
    @PostMapping("/checkProductNameUnique")
    @ResponseBody
    public String checkCategoryNameUnique(ProductSearchVO productSearchVO) {
        return productService.checkProductNameUnique(productSearchVO);
    }

    /**
     * 数据新增-添加商品
     *
     * @param productVO
     * @return
     */
    @Log(title = "总后台-商品管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:product:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ProductVO productVO) {
        ProductSearchVO productSearchVO = new ProductSearchVO();
        if (Constants.FAIL.equals(productService.checkProductNameUnique(productSearchVO))) {
            return error("新增商品【'" + productVO.getProductName() + "'】失败，商品已存在");
        }
        productVO.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        return toAjax(productService.insertProduct(productVO));
    }

    /**
     * 数据更新-编辑商品信息
     *
     * @param productVO 商品信息
     * @return
     */
    @Log(title = "总后台-商品管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:product:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ProductVO productVO) {
        ProductSearchVO productSearchVO = new ProductSearchVO();
        if (productVO.getId() != null && productVO.getId() != 0) {
            productSearchVO.setProductId(productVO.getId());
            productSearchVO.setProductName(productVO.getProductName());
            if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(productService.checkProductNameUnique(productSearchVO))) {
                return error("修改商品'" + productVO.getProductName() + "'失败，商品已存在不允许覆盖");
            }
        }
        productVO.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return AjaxResult.success(productService.updateProduct(productVO));
    }

    /**
     * 数据更新-编辑商品分类
     *
     * @param productVO 商品信息
     * @return
     */
    @Log(title = "总后台-商品分类", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:product:edit")
    @PostMapping("/editCategory")
    @ResponseBody
    public AjaxResult editCategory(ProductVO productVO) {
        productVO.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return AjaxResult.success(productService.updateProduct(productVO));
    }

    /**
     * 数据更新-删除商品
     *
     * @param ids 商品ID
     * @return
     */
    @Log(title = "总后台-商品管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:product:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult delete(String ids) {
        Long productId = Long.valueOf(ids);
        return toAjax(productService.deleteProductById(productId));
    }
}
